package cn.wolfcode.p2p.base.service.impl;

import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.mapper.VideoAuthMapper;
import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthServie;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.BitStatesUtil;
import cn.wolfcode.p2p.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service@Transactional
public class VideoAuthServieImpl implements IVideoAuthServie {
    @Autowired
    private VideoAuthMapper videoAuthMapper;
    @Autowired
    private IUserInfoService userInfoService;
    @Override
    public VideoAuth getById(Long realAuthId) {
        return videoAuthMapper.selectByPrimaryKey(realAuthId);
    }

    @Override
    public void apply(VideoAuth videoAuth) {
        //判断参数
        //判断有没有通过视频认证
        UserInfo userInfo = userInfoService.getById(UserContext.getCurrentUser().getId());
        AssertUtil.instance().isFalse(userInfo.hasVedioAuth(),"老铁,您已经通过了视频认证");
        //判断有没有视频预约
        AssertUtil.instance().isNull(userInfo.getVideAuthId(),"老铁,您的视频认证在审核中,耐心等待...");
        //第一次申请
        VideoAuth va= new VideoAuth();
        va.setOrderTime(videoAuth.getOrderTime());
        va.setOrderDate(videoAuth.getOrderDate());
        va.setApplier(UserContext.getCurrentUser());
        va.setAuditor(videoAuth.getAuditor());
        va.setApplyTime(new Date());
        videoAuthMapper.insert(va);

        //设置用户uesrinfo videoAuthId
        userInfo.setVideAuthId(va.getId());
        userInfoService.update(userInfo);

    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<VideoAuth> list = videoAuthMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void audit(Long id, Integer state, String remark) {
        //判断参数
        //是否是待审核状态
        VideoAuth videoAuth = videoAuthMapper.selectByPrimaryKey(id);
        AssertUtil.instance().isFalse(videoAuth.getState() != VideoAuth.STATE_NORMAL,"视频认证对象状态不处于待审核");
        videoAuth.setState(state);
        videoAuth.setAuditTime(new Date());
        videoAuth.setRemark(remark);
        update(videoAuth);

        UserInfo userInfo = userInfoService.getById(videoAuth.getApplier().getId());
        //审核成功
        if(state == VideoAuth.STATE_SUCCESS){
            //添加位状态
            userInfo.addBitState(BitStatesUtil.OP_VEDIO_AUTH);
        }else{
            userInfo.setVideAuthId(null);
        }
        userInfoService.update(userInfo);
    }

    public void update(VideoAuth videoAuth){
        videoAuthMapper.updateByPrimaryKey(videoAuth);
    }
}
